<?php
namespace Admin\Controller;

//通用Controller类
class CommonController extends \Think\Controller{
   
    public $info;//渲染的数据
    protected $customer;
    protected $customer_id;
    
    public function _initialize() {
        header("Content-Type: text/html; charset=utf-8");

        //判断是否登录
        $customer = session(C('ADMIN_SESSION'));
        if ((ACTION_NAME != 'login') && empty($customer)) {//frameset的掉线处理
            echo '<script>
                    if (window.top == window.self) {
                        window.location.href = "'.U('Admin/Customer/login').'";
                    } else {
                        parent.window.location.reload();
                    }
                </script>';
            exit;
        }

        $this->customer_id = $customer['id'];
        $this->customer    = D('Customer')->find($customer['id']);

        $this->info = array();
        $this->info['customer_id'] = $customer['id'];
        $this->info['customer']    = $this->customer;
        
        // 权限控制
        // $myPower = D('Power')->getPowerBySuid($suserRow['id'], $suserRow['type']);
        // if(!in_array(CONTROLLER_NAME,$myPower)) $this->error('您无此权限');
    }
    
    public function index(){
        $where = array();
        if(I('keywords')) $where['name'] = array('like','%'.I('keywords').'%');
        $dataPage = D(CONTROLLER_NAME)->getPager($where);

        $this->info['list'] = $dataPage['data'];
        $this->info['sum']  = $dataPage['sum'];
        $this->info['page'] = $dataPage['page'];

        $this->assign($this->info);
        $this->display();
    }
    
    public function save(){
        $model = D(CONTROLLER_NAME);
        if(IS_POST){
            $data = I('post.');
            if(!$model->create($data['info'])) $this->error($model->getError());
            $res = $model->saveData($data['info']);
            $res === false ? $this->error('保存失败') : $this->success('保存成功');
        }else{
            $item = array();
            if(I('id')) $item = $model->find(I('id'));
            $this->info['item'] = $item;
            $this->info['upPageUrl'] = $_SERVER['HTTP_REFERER'];

            $this->assign($this->info);
            $this->display();
        }
    }

    public function del(){
        $res = D(CONTROLLER_NAME)->delData(intval(I('request.id')));
        $res ? $this->success('删除成功') : $this->error('删除失败');
    }
    
}
